<?php
// +----------------------------------------------------------------------
// | 知识星球[基于ThinkPHP5开发]
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2019 abcblockchain.io
// +----------------------------------------------------------------------
// | 后台管理使用了HisiPHP
// +----------------------------------------------------------------------
// | Author: abcblockchain.io
// +----------------------------------------------------------------------
namespace app\common\model;

use think\Model;
use think\Db;

/**
 * Token模型
 * @package app\common\model
 */
class Currency extends Model
{
    // 定义时间戳字段名
    protected $createTime = 'ctime';
    protected $updateTime = false;

    // 自动写入时间戳
    protected $autoWriteTimestamp = true;


    /**
     * 创建代币
     * @author
     * @param $currencyName
     * @param $currencyAllNum
     * @return bool|int|string
     */
    public function createCurrency($currencyName, $currencyAllNum)
    {
        $map = [];
        $map['currency_name'] = $currencyName;
        $map['currency_all_num'] = $currencyAllNum;
        $map['status'] = 1;
        $map['ctime'] = time();
        $res = $this->insertGetId($map);
        if ($res) {
            return $res;
        } else {
            return false;
        }
    }

    /**
     * 获取币种列表
     * @author
     * @param bool $base
     * @param array $myCurrency
     * @param int $startNo
     * @param int $limit
     * @return array|false|\PDOStatement|string|\think\Collection
     */
    public function currencyList($startNo = 0, $limit = 10, $search='', $base = false, $myCurrency=[])
    {
        $map['contract_address'] = array('neq','');
        $map['status'] = 1;
        $startNo = $startNo * $limit;
        if (!$base) {
            $map['id'] = array('gt', 2);
        }
        if(!empty($myCurrency)){
            $map['id'] = array('in',$myCurrency);
        }
        if(!empty($search)){
            $map1 = $map;
            $map2 = $map;
            $map3 = $map;
            $map1['currency_mark'] = array('like',"%$search%");
            $map2['currency_name'] = array('like',"%$search%");
            $map3['contract_address'] = $search;
            $currencyList = $this->where(function($query) use ($map1) { $query->where($map1);})->whereOr(function($query) use ($map2) { $query->where($map2);})->whereOr(function($query) use ($map3) { $query->where($map3);})->field('id,currency_name,currency_mark,logo,currency_all_num,bcv_address,contract_address,last_price,ctime')->limit($startNo, $limit)->select();
        }else{
            $currencyList = $this->where($map)->field('id,currency_name,currency_mark,logo,currency_all_num,bcv_address,contract_address,last_price,ctime,change,volume')->limit($startNo, $limit)->select();
        }
        return $currencyList ? $currencyList : [];
    }

    /**
     * 获取币种信息
     * @author
     * @param $currencyId
     * @param $contractAddress
     * @return array|false|\PDOStatement|string|Model
     */
    public function currencyInfo($currencyId='', $contractAddress=''){
        if($currencyId){
            $map['id'] = $currencyId;
        }
        if($contractAddress){
            $map['contract_address'] = $contractAddress;
        }
        $currencyInfo = $this->where($map)->field('id,currency_name,currency_mark,logo,currency_all_num,bcv_address,contract_address,last_price,ctime,fee,change,volume')->find();
        return $currencyInfo ? $currencyInfo : [];
    }

}